package com.dht.xiao.jianye.utils;

import android.os.Handler;
import android.util.Log;

import com.dlc.serialportlibrary.DLCSerialPortUtil;
import com.dlc.serialportlibrary.ReceiveCallback;
import com.dlc.serialportlibrary.SerialPortManager;

/**
 * Created by Administrator on 2018/12/7/007.
 */

public class SerialPortUtil {
    private static volatile SerialPortUtil portUtil = null;

    SerialPortManager manager;
    DataReceiverImpl mDataReceiver;
    static Handler mHandler;
    private OperationResult operationResult;
    private HexTool mHexTool = HexTool.getInstance();
    public static SerialPortUtil getInstance(Handler handler) {
        if (portUtil == null) {
            synchronized (SerialPortUtil.class) {
                if (portUtil == null) {
                    portUtil = new SerialPortUtil();
                }
            }
        }
        if (mHandler == null) {
            mHandler = handler;
        }
        return portUtil;
    }
    public void setCallBack(OperationResult result) {
        operationResult = result;
    }

    public interface OperationResult {
        void receiveResult(String command, byte[] allData);

    }
    StringBuilder sb = new StringBuilder();
    private SerialPortUtil() {

        mDataReceiver = new DataReceiverImpl(mHandler) {
            @Override
            public void onReceive(String command, byte[] allData) {
                if (operationResult != null){
                    operationResult.receiveResult(command,allData);
                }
            }
            @Override
            public void resetCache() {

            }
        };

        try {
            manager = DLCSerialPortUtil.getInstance().open("/dev/ttyS4", "9600");///dev/ttyUSB0
            if (manager.isOpenSuccess()) {
                Log.e("11","串口打開成功");
                manager.setReceiveCallback(new ReceiveCallback() {
                    @Override
                    public void onReceive(String devicePath, String baudrateString,final byte[] received,
                        int size) {
                        if (mDataReceiver != null) {
                            String string = ByteUtil.bytes2HexStr(received, 0, size);
                            mDataReceiver.onReceive(received, 0, size);
                        }
//                        String ss = "";
//                        try {
//                            ss = ByteUtil.bytes2HexStr(received, 0, size);
//                           // LogToFile.e("读到命令="+ss);
//                            sb.append(ss);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//
//                        mHandler.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//
//                                if (operationResult != null){
//                                    if (sb.length() > 10) {
//                                        LogToFile.e("读到完整命令=" + sb.toString());
//                                    }
//                                    operationResult.receiveResult(sb.toString(),received);
//                                }
//                                sb = new StringBuilder();
//                            }
//                        },500);



                    }
                });

            }else {
                Log.e("11","串口打開失敗");
            }
        }catch (Exception e){

        }

    }

    public void open_door(int num){
        try {
        StringBuilder chickLock = new StringBuilder();
        chickLock.append("574B4C59")
            .append("09")
            .append("01")
            .append("82")
            .append(getNumber(num));//
        //3AA3 00000000 0001 0018 09CA 01 1142 5420 01 000003E8010601f7
        //3AA3 00000000 0001 0018 09CA 01
        String checkCode = mHexTool.getXOR(chickLock.toString());
        chickLock.append(checkCode);
        String string = chickLock.toString();

            manager.sendData(HexTool.getInstance().hexStringToBytes(string));
           // Log.e("))))))))))发送命令==", string);
            LogToFile.e(string + "硬件发送开锁");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void inquire_door(int num){
        StringBuilder chickLock = new StringBuilder();
        chickLock.append("574B4C59")
            .append("09")
            .append("01")
            .append("83")
            .append(getNumber(num));//
        //3AA3 00000000 0001 0018 09CA 01 1142 5420 01 000003E8010601f7
        //3AA3 00000000 0001 0018 09CA 01
        String checkCode = mHexTool.getXOR(chickLock.toString());
        chickLock.append(checkCode);
        String string = chickLock.toString();
        try {
            manager.sendData(HexTool.getInstance().hexStringToBytes(string));
           // Log.e("))))))))))发送命令==", string);
            LogToFile.e(string + "硬件发送查询锁的状态");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getNumber(int num){
        String string = "";

        return ByteUtil.decimal2fitHex(num);
    }



}
